package com.ruoyi.common.utils;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.WKTWriter;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description geojson转化为wkt格式
 * @Author wsl
 * @Date 2025-02-10 19:37
 * @Version 1.0
 */

public class GeoJsonUtils {

    public static String convertGeoJsonToWkt(String geoJson) throws IOException {
        // 创建 GeometryJSON 对象用于解析 GeoJSON
        GeometryJSON gjson = new GeometryJSON();
        // 解析 GeoJSON 字符串为 Geometry 对象
        Geometry geometry = gjson.read(geoJson);
        // 创建 WKTWriter 对象用于将 Geometry 对象转换为 WKT 字符串
        WKTWriter writer = new WKTWriter();
        // 将 Geometry 对象转换为 WKT 字符串
        return writer.write(geometry);
    }

    public static String getOuterBoundaryGeoJSON(String shpFilePath) throws IOException {
        File file = new File(shpFilePath);
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();
        SimpleFeatureCollection featureCollection = featureSource.getFeatures();
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        List<Polygon> polygons = new ArrayList<>();
        SimpleFeatureIterator iterator = featureCollection.features();
        try {
            while (iterator.hasNext()) {
                SimpleFeature feature = iterator.next();
                Object geometryAttribute = feature.getDefaultGeometry();
                if (geometryAttribute instanceof Polygon) {
                    Polygon polygon = (Polygon) geometryAttribute;
                    // 尝试修复几何图形
                    Geometry repaired = polygon.buffer(0);
                    if (repaired instanceof Polygon) {
                        polygons.add((Polygon) repaired);
                    } else if (repaired instanceof MultiPolygon) {
                        MultiPolygon multiPolygon = (MultiPolygon) repaired;
                        for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
                            Geometry geometry = multiPolygon.getGeometryN(i);
                            if (geometry instanceof Polygon) {
                                polygons.add((Polygon) geometry);
                            }
                        }
                    }
                } else if (geometryAttribute instanceof MultiPolygon) {
                    MultiPolygon multiPolygon = (MultiPolygon) geometryAttribute;
                    // 尝试修复几何图形
                    Geometry repaired = multiPolygon.buffer(0);
                    if (repaired instanceof Polygon) {
                        polygons.add((Polygon) repaired);
                    } else if (repaired instanceof MultiPolygon) {
                        multiPolygon = (MultiPolygon) repaired;
                        for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
                            Geometry geometry = multiPolygon.getGeometryN(i);
                            if (geometry instanceof Polygon) {
                                polygons.add((Polygon) geometry);
                            }
                        }
                    }
                }
            }
        } finally {
            iterator.close();
        }
        Geometry[] geometries = polygons.toArray(new Geometry[0]);
        GeometryCollection geometryCollection = geometryFactory.createGeometryCollection(geometries);
        // 合并所有多边形
        Geometry union = geometryCollection.union();
        // 提取最外层边界
        Geometry outerBoundary = null;
        if (union instanceof Polygon) {
            outerBoundary = ((Polygon) union).getExteriorRing();
        } else if (union instanceof MultiPolygon) {
            outerBoundary = ((MultiPolygon) union).getBoundary();
        }
        SimpleFeatureType featureType = featureSource.getSchema();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
        featureBuilder.add(outerBoundary);
        SimpleFeature feature = featureBuilder.buildFeature(null);
        FeatureJSON featureJSON = new FeatureJSON();
        StringWriter writer = new StringWriter();
        featureJSON.writeFeature(feature, writer);
        store.dispose();
        return writer.toString();
    }

    public static void main(String[] args) {
        String shpFilePath = "D:\\西安项目\\西安项目_成果数据\\西安项目_成果数据\\2_功能分区\\1_华盛顿_zoning.shp";
        try {
            String geoJSON = getOuterBoundaryGeoJSON(shpFilePath);
            System.out.println(geoJSON);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    }



